package ysu.sxm.ptjc.Service.Impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ysu.sxm.ptjc.Dao.DefectTypeInfoDao;
import ysu.sxm.ptjc.Dao.OxidationDefectInfoDao;
import ysu.sxm.ptjc.Dao.PolishingDefectInfoDao;
import ysu.sxm.ptjc.Dao.produceInfoDao;
import ysu.sxm.ptjc.Domin.OxidationLineReq;
import ysu.sxm.ptjc.Domin.baseInput;
import ysu.sxm.ptjc.Service.GetOxidationLineService;
import ysu.sxm.ptjc.Domin.baseOutput;
import ysu.sxm.ptjc.pojo.DefectTypeInfo;
import ysu.sxm.ptjc.pojo.ProduceInfo;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class GetOxidationLineServiceimpl implements GetOxidationLineService {
    @Autowired
    produceInfoDao produceInfoDao;
    @Autowired
    DefectTypeInfoDao defectTypeInfoDao;
    @Autowired
    OxidationDefectInfoDao oxidationDefectInfoDao;
    @Autowired
    PolishingDefectInfoDao polishingDefectInfoDao;
    @Override
    public List<baseOutput> getDate(OxidationLineReq oxidationLineReq) throws ParseException {

        List<Integer>months=oxidationLineReq.getMonth();
        int year=oxidationLineReq.getYear();
        List<Integer>lineIds=oxidationLineReq.getLineId();
        List<String>types=oxidationLineReq.getType();
        List<baseOutput>outputs=new ArrayList<baseOutput>();
        baseOutput Output=new baseOutput();
        for(int month:months){
            List<Date>dates=getTheData(year,month);
            for(Date date:dates){
               // System.out.println(date );
               baseInput input=new baseInput();
               input.setLine_id(lineIds);
               input.setProduce_type(types);
               input.setProduce_date(date);
               // System.out.println("111");
               int total=produceInfoDao.getnumByOthers(input);
               List<DefectTypeInfo> defectTypeInfos=defectTypeInfoDao.getByBaseInput(input);

             //System.out.println(defectTypeInfos);

                if(!defectTypeInfos.isEmpty()) {
                    List<Integer> Furroad_Brokens = oxidationDefectInfoDao.getFurroadBroken(defectTypeInfos);
                    List<Integer> Polishing_Grains = polishingDefectInfoDao.getPolishingGrain(defectTypeInfos);
                    int Furroad_Broken=0;int Polishing_Grain=0;
                    for (int n:Furroad_Brokens){
                        Furroad_Broken+=n;
                    }
                    for(int n:Polishing_Grains){
                        Polishing_Grain+=n;
                    }
                    if(Furroad_Broken!=0&&total!=0){
                        Output.setFurroad_broken((double)Furroad_Broken/(double)total);
                    }else{
                        Output.setFurroad_broken(0);
                    }
                    if(Polishing_Grain!=0&&total!=0){
                        Output.setPolished_grain((double)Polishing_Grain/(double)total);
                    }else {
                        Output.setPolished_grain(0);
                    }
                }else {
                    Output.setPolished_grain(0);
                    Output.setFurroad_broken(0);
                }
                Output.setDate(date);
                //System.out.println(Output);
                //System.out.println(Output);
                outputs.add(Output);
                Output=new baseOutput();
            }
        }
        //System.out.println(outputs);
        return outputs;

    }

    @Override
    public List<Date> getTheData(int year , int month) throws ParseException {
        List<Date>list=new ArrayList<Date>();
        int days=0;
        if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)days=31;
        else if(month==2){
            if((year%4==0&&year%100!=0)||year%400==0)days=29;
            else days=28;
        }
        else days=30;
        for(int i=1;i<=days;i++){

            String dateStr = year+"-"+month+"-"+i;
            Date result = Date.valueOf(dateStr);
            list.add(result);
        }
        return list;
    }
}
